package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class AddressBookServiceImpl implements AddressBookService {

    private final AddressBookMapper addressBookMapper;


    //查询当前登录用户的所有地址信息
    public List<AddressBook> getAll() {
        return addressBookMapper.getAll(BaseContext.getCurrentId());
    }

    //设置默认地址
    public void setDefaltAddress(Long id) {
        addressBookMapper.clearDefaultAddress(BaseContext.getCurrentId());
        addressBookMapper.setDefaltAddress(id);
    }

    //根据id查询地址
    public AddressBook getById(Long id) {
        return addressBookMapper.getById(id);
    }

    //新增地址
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.add(addressBook);
    }

    //根据id删除地址
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    //根据id修改地址
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    //查询默认地址
    public AddressBook getDefaultAddress(Long currentId) {
        return addressBookMapper.getDefaultAddress(currentId);
    }


}
